package androidx.lifecycle;

import androidx.lifecycle.e;
import b.b.a.b.b;
import java.util.Map;

public abstract class LiveData<T> {
    static final Object i = new Object();

    /* renamed from: a  reason: collision with root package name */
    final Object f716a = new Object();

    /* renamed from: b  reason: collision with root package name */
    private b<n<? super T>, LiveData<T>.a> f717b = new b<>();

    /* renamed from: c  reason: collision with root package name */
    int f718c = 0;

    /* renamed from: d  reason: collision with root package name */
    private volatile Object f719d = i;
    volatile Object e = i;
    private int f = -1;
    private boolean g;
    private boolean h;

    class LifecycleBoundObserver extends LiveData<T>.a implements f {
        final h e;

        LifecycleBoundObserver(h hVar, n<? super T> nVar) {
            super(nVar);
            this.e = hVar;
        }

        public void d(h hVar, e.a aVar) {
            if (this.e.getLifecycle().b() == e.b.DESTROYED) {
                LiveData.this.i(this.f720a);
            } else {
                h(k());
            }
        }

        /* access modifiers changed from: package-private */
        public void i() {
            this.e.getLifecycle().c(this);
        }

        /* access modifiers changed from: package-private */
        public boolean j(h hVar) {
            return this.e == hVar;
        }

        /* access modifiers changed from: package-private */
        public boolean k() {
            return this.e.getLifecycle().b().a(e.b.STARTED);
        }
    }

    private abstract class a {

        /* renamed from: a  reason: collision with root package name */
        final n<? super T> f720a;

        /* renamed from: b  reason: collision with root package name */
        boolean f721b;

        /* renamed from: c  reason: collision with root package name */
        int f722c = -1;

        a(n<? super T> nVar) {
            this.f720a = nVar;
        }

        /* access modifiers changed from: package-private */
        public void h(boolean z) {
            if (z != this.f721b) {
                this.f721b = z;
                int i = 1;
                boolean z2 = LiveData.this.f718c == 0;
                LiveData liveData = LiveData.this;
                int i2 = liveData.f718c;
                if (!this.f721b) {
                    i = -1;
                }
                liveData.f718c = i2 + i;
                if (z2 && this.f721b) {
                    LiveData.this.g();
                }
                LiveData liveData2 = LiveData.this;
                if (liveData2.f718c == 0 && !this.f721b) {
                    liveData2.h();
                }
                if (this.f721b) {
                    LiveData.this.c(this);
                }
            }
        }

        /* access modifiers changed from: package-private */
        public void i() {
        }

        /* access modifiers changed from: package-private */
        public boolean j(h hVar) {
            return false;
        }

        /* access modifiers changed from: package-private */
        public abstract boolean k();
    }

    static void a(String str) {
        if (!b.b.a.a.a.b().a()) {
            throw new IllegalStateException("Cannot invoke " + str + " on a background thread");
        }
    }

    private void b(LiveData<T>.a aVar) {
        if (aVar.f721b) {
            if (!aVar.k()) {
                aVar.h(false);
                return;
            }
            int i2 = aVar.f722c;
            int i3 = this.f;
            if (i2 < i3) {
                aVar.f722c = i3;
                aVar.f720a.a(this.f719d);
            }
        }
    }

    /* access modifiers changed from: package-private */
    public void c(LiveData<T>.a aVar) {
        if (this.g) {
            this.h = true;
            return;
        }
        this.g = true;
        do {
            this.h = false;
            if (aVar == null) {
                b<K, V>.d d2 = this.f717b.d();
                while (d2.hasNext()) {
                    b((a) ((Map.Entry) d2.next()).getValue());
                    if (this.h) {
                        break;
                    }
                }
            } else {
                b(aVar);
                aVar = null;
            }
        } while (this.h);
        this.g = false;
    }

    public T d() {
        T t = this.f719d;
        if (t != i) {
            return t;
        }
        return null;
    }

    public boolean e() {
        return this.f718c > 0;
    }

    public void f(h hVar, n<? super T> nVar) {
        a("observe");
        if (hVar.getLifecycle().b() != e.b.DESTROYED) {
            LifecycleBoundObserver lifecycleBoundObserver = new LifecycleBoundObserver(hVar, nVar);
            a g2 = this.f717b.g(nVar, lifecycleBoundObserver);
            if (g2 != null && !g2.j(hVar)) {
                throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
            } else if (g2 == null) {
                hVar.getLifecycle().a(lifecycleBoundObserver);
            }
        }
    }

    /* access modifiers changed from: protected */
    public void g() {
    }

    /* access modifiers changed from: protected */
    public void h() {
    }

    public void i(n<? super T> nVar) {
        a("removeObserver");
        a h2 = this.f717b.h(nVar);
        if (h2 != null) {
            h2.i();
            h2.h(false);
        }
    }

    /* access modifiers changed from: protected */
    public void j(T t) {
        a("setValue");
        this.f++;
        this.f719d = t;
        c((LiveData<T>.a) null);
    }
}
